<?PHP

class Admin_send_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','配送管理');
		$this->assign('menuid1','3'); 
		$this->assign('menuid2','4'); 
	}
	
	 

	/* 配送明细 */
	public function index__(){
		//权限判断
		factory::module('admin')->authSys('plan'); 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
		

			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			
			
  	  #查询条件
			$sql_where = ' where   b.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'b.code', 	'd.name'	,'b.senddate' 	,'c.username','c.name' ));  
			if($_POST['status']!=''){
				$sql_where .= ' and b.status= "'.$_POST['status'].'"';	
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and b.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and b.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];		
		  
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_send` a 
			left join t_sendpi b on a.sendpi_id=b.id 
			left join t_user c on a.user_id=c.id 
			left join t_sendfen d on a.sendfen_id=d.id
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*,
					b.status,
					b.senddate,
					b.code,				
					c.username,
					c.name,
					d.name as fenzhi_name
				from `t_send` a 
				left join t_sendpi b on a.sendpi_id=b.id 
				left join t_user c on a.user_id=c.id 
				left join t_sendfen d on a.sendfen_id=d.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as $k=>&$v){
					$v['veges'] = factory::module('packet')->decode($v['packet']);
					$v['veges_string'] = factory::module('packet')->decode($v['packet'],'string');
					$v['veges_html'] = factory::module('packet')->decode($v['packet'],'html');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
	}


	# 修改配送批次中的某一个会员配送内容，固定项目是不能修改 ，只能修改可选项目，如果该配送批次没有可选项，那么修改操作就完全不能操作任何数据！
	public function modify__(){ 
		//权限判断
		factory::module('admin')->authSys('plan'); 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$vegeids = format::addslash_json(json_decode( stripslashes($_POST['vegeids']) ));
			$_POST=format::addslash_deep($_POST);
		  $send_id = $_POST['send_id'];
			$db = factory::getDBO();
			$rs_send = $db->loadassoc('select * from t_send where id="'.$send_id.'"');
			$rs_sendpi = $db->loadassoc('select * from t_sendpi where id="'.$rs_send['sendpi_id'].'" and deleted=0 ');
			$rs_sendfen = $db->loadassoc('select * from t_sendfen where id="'.$rs_send['sendfen_id'].'" ');
			if(!$rs_send || !$rs_sendpi || !$rs_sendfen){
				exit('操作失败，没有找到处理记录！');
			}
			if($rs_sendpi['status']=='1'){
				exit('操作失败，当前所属配送批次正在生产中！');
			}
			if($rs_sendpi['status']=='2'){
				exit('操作失败，当前所属配送批次已经完成！');
			}
			if(!factory::module('vege')->updatesltvege($send_id,$vegeids)){
				exit(factory::module('vege')->getError());
			}
			exit('{success:1,msg:"操作成功！"}');
		}	
		$_GET=format::addslash_deep($_GET);
		$send_id = intval($_GET['id']);
		$db = factory::getDBO();
		
	  # 配送信息
		$rs_send = $db->loadassoc('select a.*,b.username,b.name from `t_send` a left join t_user b on a.user_id=b.id where  a.id="'.$send_id.'" ');
		if(!$rs_send){
			exit('没有查到任何记录！');
		}
		$this->assign('rs_send',$rs_send);
		
	  # 配送批次信息
		$rs_sendpi = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendpi` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.deleted=0 and a.id="'.$rs_send['sendpi_id'].'" ');
		if(!$rs_sendpi){
			exit('没有查到任何记录！');
		}
		if($rs_sendpi['status']=='1'){
			exit('操作失败，当前所属配送批次正在生产中！');
		}
		if($rs_sendpi['status']=='2'){
			exit('操作失败，当前所属配送批次已经完成！');
		}
		$this->assign('rs_sendpi',$rs_sendpi);
		
				
	  # 配送分支信息
		$rs_sendfen = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendfen` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.id="'.$rs_send['sendfen_id'].'" ');
		if(!$rs_sendfen){
			exit('没有查到任何记录！');
		}
		$this->assign('rs_sendfen',$rs_sendfen);
		 
		 
		# 当前会员配送的实际选择菜品信息
		$rs_send_vege = $db->loadassoclist('select a.*,b.code,b.name from `t_send_vege` a left join t_vege b on a.vege_id=b.id  where a.send_id="'.$rs_send['id'].'" ');
		$this->assign('rs_send_vege',$rs_send_vege);
		
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
		
		   
	}




 
}
?>